﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Diagnostics;

namespace Axe.Web.HttpModules
{
    /// <summary>
    /// Страница, которая умеет определять, был-ли произведен Postback или
    /// пользователь надавил F5
    /// </summary>
    public class RefreshCheckSupportPage : Page
    {
        #region public bool IsRefreshed
        public bool IsRefreshed
        {
            get
            {
                try
                {
                    HttpContext ctx = HttpContext.Current;
                    if (!ctx.Items.Contains(RefreshAction.PageRefreshEntry))
                        return false;

                    object o = ctx.Items[RefreshAction.PageRefreshEntry];
                    if (o == null)
                        return false;
                    return (bool)o;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                    return false;
                }
            }
        }
        #endregion

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);
            SaveRefreshState();
        }

        #region private void SaveRefreshState()
        private void SaveRefreshState()
        {
            try
            {
                
                HttpContext ctx = HttpContext.Current;
                if (ctx.Items.Contains(RefreshAction.NextPageTicketEntry))
                {
                    int ticket = (int)ctx.Items[RefreshAction.NextPageTicketEntry];
                    ClientScript.RegisterHiddenField(RefreshAction.CurrentRefreshTicketEntry, ticket.ToString());
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
        #endregion
    }
}
